<?php
/*
Plugin Name: 长尾巴(Long Tail)
Plugin URI: http://mifunny.info/long-tail-120.html
Description: 在theme的&lt;/head&gt;和&lt;/body&gt;之前加入些许调料。  必须是等级 8 以上的用户才可使用。(0.3版后可以直接使用php代码)
Version: 0.3
Author: LD King
Author URI: http://mifunny.info
License: GPL v3
*/

/**
 * 添加博客尾巴信息^{^
 *
 */
class Tail {
	
	/**
	 * 初始化尾巴，加入action
	 * add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] );
	 * 
	 */
	function init() {
		$tail = get_option('head_tail');
		if (!empty($tail)) {
			add_action('wp_head', array($this, 'addHead'), 10000);
		}
		$tail = get_option('body_tail');
		if (!empty($tail)) {
			add_action('wp_footer', array($this, 'addBody'), 10000);
		}
	} /* END func init */
	
	/**
	 * 调用WordPress内置行为钩子(action hook): wp_head
	 *
	 */
	function addHead() {
		eval(" ?><!-- Begin: Long Tail -->\n".get_option('head_tail')."\n<!-- End: Long Tail -->\n<?php ");
	}
	
	/**
	 * 调用WordPress内置行为钩子(action hook): wp_footer
	 *
	 */
	function addBody() {
		eval(" ?><!-- Begin: Long Tail -->\n".get_option('body_tail')."\n<!-- End: Long Tail -->\n<?php ");
	}
	
	/**
	 * 添加配置菜单
	 * add_options_page(page_title, menu_title, access_level/capability, file, [function]);
	 * 
	 */
	function addOptionsPage() {
		add_options_page('Long Tail', 'Long Tail', 8, __FILE__, array(&$this, 'optionPage'));
	}
	
	/**
	 * 尾巴 选项
	 * 
	 */
	function optionPage() {
		?>
<div class="wrap">
<h2><?php _e('Long Tail', 'long-tail'); ?></h2>
<form method="post" action="options.php"><?php wp_nonce_field('update-options') ?>
	<table class="form-table">
	<tr valign="top"><th scope="row"><?php _e('Head Tail', 'long-tail'); ?></th><td>
	<fieldset><legend class="hidden"><?php _e('Head Tail', 'long-tail'); ?></legend>
		<p><label for="head_tail"><?php _e('Fill your Head-Tail in the text box below. (Display at the end of the <span style="color:blue">&lt;head&gt;</span> tag.)', 'long-tail'); ?></label></p>
		<p><textarea name="head_tail" cols="60" rows="10" id="head_tail" style="width: 98%; font-size: 12px;" class="code"><?php echo get_option('head_tail'); ?></textarea></p>
	</fieldset></td></tr>
	<tr valign="top"><th scope="row"><?php _e('Body Tail', 'long-tail'); ?></th><td>
	<fieldset><legend class="hidden"><?php _e('Body Tail', 'long-tail'); ?></legend>
		<p><label for="body_tail"><?php _e('Fill your Body-Tail in the text box below. (Display at the end of the <span style="color:blue">&lt;body&gt;</span> tag.)', 'long-tail'); ?></label></p>
		<p><textarea name="body_tail" cols="60" rows="10" id="body_tail" style="width: 98%; font-size: 12px;" class="code"><?php echo get_option('body_tail'); ?></textarea></p>
	</fieldset></td></tr>
	</table>
<p class="submit">
	<input type="hidden" name="action" value="update" />
	<input type="hidden" name="page_options" value="head_tail,body_tail" />
	<input type="submit" name="Submit" value="<?php _e('Update Options »', 'long-tail'); ?>" />
</p>
</form>
</div><!-- wrap -->
<?php
	} /* END func optionPage */
} /* END class Tail */

$tail = new Tail();
$tail->init();
add_action('admin_menu', array($tail, 'addOptionsPage'));
unset($tail);
?>